package managedbean.SCM.MaterialOrder;

import entity.SCM.MaterialLine;
import entity.SCM.MaterialOrder;
import entity.RawMaterial;
import entity.SCM.Supplier;
import entity.SCM.SupplyItem;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.PhaseEvent;
import session.stateless.SCM.MaterialOrderBeanLocal;
import session.stateless.SCM.SupplierBeanLocal;

@ManagedBean (name="editMaterialOrder")
@ViewScoped
public class EditMaterialOrderManagedBean implements Serializable{

    @EJB
    private MaterialOrderBeanLocal materialOrderBeanLocal;
    @EJB
    private SupplierBeanLocal supplierBeanLocal;
    private MaterialOrder materialOrderToEdit;
    private List<String> supplierNames = new ArrayList<String>();
    private List<String> materialNames = new ArrayList<String>();
    private MaterialLine selectedMaterialLine = new MaterialLine();
    private String statusMessage;
    private List<MaterialLine> list;
    private String selectedRawMaterial;
    private Long lotQuantity;
    private List<MaterialLine> temp = new ArrayList<MaterialLine>();

    public EditMaterialOrderManagedBean() {
    }

    public void initView(PhaseEvent event){
        if(materialOrderToEdit==null){
            materialOrderToEdit=(MaterialOrder)FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("selectedMaterialOrder");           
            if(materialOrderToEdit==null){
                System.out.println("Please choose Material Order First");
            }else{
                this.setList(materialOrderToEdit.getMaterialLines());
                this.setMaterialNames(materialOrderToEdit.getSupplier().getSupplierName());
            }
        }
    }

    public String getSelectedRawMaterial() {
        return selectedRawMaterial;
    }

    public void setSelectedRawMaterial(String selectedRawMaterial) {
        this.selectedRawMaterial = selectedRawMaterial;
    }

    public Long getLotQuantity() {
        return lotQuantity;
    }

    public void setLotQuantity(Long lotQuantity) {
        this.lotQuantity = lotQuantity;
    }

    public List<MaterialLine> getList() {
        return list;
    }

    public void setList(List<MaterialLine> list) {
        this.list = list;
    }

    public MaterialOrder getMaterialOrderToEdit() {
        return materialOrderToEdit;
    }

    public void setMaterialOrderToEdit(MaterialOrder materialOrderToEdit) {
        this.materialOrderToEdit = materialOrderToEdit;
    }

    public MaterialLine getSelectedMaterialLine() {
        return selectedMaterialLine;
    }

    public void setSelectedMaterialLine(MaterialLine selectedMaterialLine) {
        this.selectedMaterialLine = selectedMaterialLine;
    }

    public List<String> getSupplierNames() {
        List<Supplier> tempS = new ArrayList<Supplier>();
        tempS = supplierBeanLocal.getAllSupplier();
        for(int i=0; i<tempS.size();i++){
            supplierNames.add(tempS.get(i).getSupplierName());
        }
        return supplierNames;
    }
    
    public List<String> getMaterialNames(){
        return materialNames;
    }
    
    public void setMaterialNames(String sn){
        Supplier s = supplierBeanLocal.getSupplier(sn);
        List<SupplyItem> sil = s.getSupplyItemList();
        for(int i=0; i<sil.size();i++){
            materialNames.add(sil.get(i).getSupplyItemType());
        }
    }

    public void addMaterialLine(ActionEvent event){
        MaterialLine tempM = new MaterialLine();
        RawMaterial rm = this.materialOrderBeanLocal.getMaterial(selectedRawMaterial);
        tempM.setRawMaterial(rm);
        tempM.setLotQuantity(lotQuantity);
        this.list.add(tempM);
    }

    public void addBack(ActionEvent event){
        System.out.print("Add Back test");
        if(this.materialOrderToEdit.getMaterialRequestTemp()==null){
            statusMessage = "This material order is created directly, no corresponding material request";
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,
                    statusMessage, ""));
        }else{
            if(this.selectedMaterialLine!=null & this.list.contains(selectedMaterialLine)){
                 temp.add(selectedMaterialLine); 
                 this.list.remove(selectedMaterialLine);
                 selectedMaterialLine = new MaterialLine();
                 statusMessage = "Material Line has been moved back to Material Request successfully";
                 FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,
                    statusMessage, ""));
            }
        }
    }
    
    public void deleteMaterialLine(ActionEvent event){
        System.out.print("Delete Material Line Test");
        if(this.selectedMaterialLine!=null){
            this.list.remove(selectedMaterialLine);
            selectedMaterialLine = new MaterialLine();
        }
    }

    public void updateMaterialOrder(ActionEvent event){
        try{
            if(this.list==null){
                statusMessage = "Material Purchasing Order is Empty";
            }else{
                if(materialOrderBeanLocal.updateMaterialOrder(materialOrderToEdit.getMaterialOrderId(), materialOrderToEdit.getSupplier().getSupplierName(), this.list,temp)==true){
                    statusMessage = "Update Raw Material Purchasing Order Successfully";
                    FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,
                    statusMessage, ""));
                }else{
                    statusMessage = "Update Raw Material Purchasing Order Unsuccessfully";
                    FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,
                    statusMessage, ""));
                }
            }           
        }catch(Exception ex){
            System.out.print("Update Material Order Exception");
        }
    }

}
